package edu.jmu.bean.message;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * 
 * @author Geven7
 *
 */
@Entity
public class Message implements java.io.Serializable {
	private static final long serialVersionUID = -6470525670484144417L;
	private Integer id;
	private Message_Type messageType = Message_Type.General;
	private String title;
	private String content;
	private Date beginTime;
	private Date endTime;
	private Integer ifPublish;  //0未发布，1已发布
	private Date recordTime;

	private Set<MessageToUser> messageToUsers = new HashSet<MessageToUser>();
	
	// Property accessors
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Enumerated(EnumType.STRING)
	@Column(length = 10)
	public Message_Type getMessageType() {
		return messageType;
	}

	public void setMessageType(Message_Type messageType) {
		this.messageType = messageType;
	}

	@Column(nullable = false, length = 100)
	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Column(nullable = false, length = 500)
	public String getContent() {
		return this.content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Temporal(TemporalType.TIMESTAMP)
	public Date getBeginTime() {
		return this.beginTime;
	}

	public void setBeginTime(Date beginTime) {
		this.beginTime = beginTime;
	}

	@Temporal(TemporalType.TIMESTAMP)
	public Date getEndTime() {
		return this.endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	@Column(nullable = false)
	public Integer getIfPublish() {
		return this.ifPublish;
	}

	public void setIfPublish(Integer ifPublish) {
		this.ifPublish = ifPublish;
	}

	@Temporal(TemporalType.TIMESTAMP)
	public Date getRecordTime() {
		return this.recordTime;
	}

	public void setRecordTime(Date recordTime) {
		this.recordTime = recordTime;
	}

	@OneToMany(cascade = {CascadeType.REFRESH,CascadeType.PERSIST}, mappedBy = "message")
	public Set<MessageToUser> getMessageToUsers() {
		return this.messageToUsers;
	}

	public void setMessageToUsers(Set<MessageToUser> messageToUsers) {
		this.messageToUsers = messageToUsers;
	}
	
	public void addMessageToUser(MessageToUser messageToUser){
		messageToUsers.add(messageToUser);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Message other = (Message) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
}